//---------------------------------------------------------------------------
// <copyright file="RibbonComboBoxItem.cs" company="Microsoft Corporation">
//     Copyright (C) Microsoft Corporation.  All rights reserved.
// </copyright>
//---------------------------------------------------------------------------

namespace Microsoft.Windows.Controls.Ribbon
{
    #region Using declarations

    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    #endregion

    /// <summary>
    ///   Implements a RibbonComboBoxItem which sends special notifications for mouse
    ///   leave events.
    /// </summary>
    public class RibbonComboBoxItem : ComboBoxItem
    {
        #region Constructors

        /// <summary>
        ///   Initializes static members of the RibbonComboBoxItem class.  Overrides
        ///   the default style.
        /// </summary>
        static RibbonComboBoxItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(RibbonComboBoxItem), new FrameworkPropertyMetadata(typeof(RibbonComboBoxItem)));
        }

        /// <summary>
        ///   Initializes a new instance of the RibbonComboBoxItem class.  Adds a property
        ///   changed callback to IsHighlighted.
        /// </summary>
        public RibbonComboBoxItem()
        {
            DependencyPropertyDescriptor isHighlightedDescriptor = DependencyPropertyDescriptor.FromProperty(IsHighlightedProperty, typeof(RibbonComboBoxItem));
            isHighlightedDescriptor.AddValueChanged(this, OnIsHighlightedChanged);
        }

        #endregion

        #region Private Properties

        /// <summary>
        ///   Gets the parent RibbonComboBox of this item.
        /// </summary>
        private RibbonComboBox ParentRibbonComboBox
        {
            get { return ItemsControl.ItemsControlFromItemContainer(this) as RibbonComboBox; }
        }

        #endregion

        #region Protected Methods

        /// <summary>
        ///   Called when the mouse pointer leaves the RibbonComboBoxItem.  In this case
        ///   we set IsHighlighted to false to mimic the Office behavior.
        /// </summary>
        /// <param name="e">The event data.</param>
        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);

            this.IsHighlighted = false;
        }

        #endregion

        #region Private Methods

        /// <summary>
        ///   Called when the IsHighlighted property changes.  In this case we want
        ///   to cancel the preview for this item.
        /// </summary>
        /// <param name="sender">The item whose IsHighlighted property has changed.</param>
        /// <param name="e">The event data.</param>
        private static void OnIsHighlightedChanged(object sender, EventArgs e)
        {
            RibbonComboBoxItem item = sender as RibbonComboBoxItem;
            RibbonCommandAction action = RibbonCommandAction.Preview;

            if ((bool)item.IsHighlighted == false)
            {
                action = RibbonCommandAction.CancelPreview;
            }

            item.ParentRibbonComboBox.ExecuteCommandFromComboBoxItem(action, item);
        }

        #endregion
    }
}